package main

import (
	"fmt"
	"logagent/Etcd"
	"logagent/Kafka"
	"logagent/Tailfile"
	"os"

	"gopkg.in/ini.v1"
)

type Config struct {
	KafkaConfig `ini:"kafka"`
	Collect     `ini:"collect"`
	EtcdConfig  `ini:"etcd"`
}
type KafkaConfig struct {
	Address  string `ini:"address"`
	Topic    string `ini:"topic"`
	ChanSize int    `ini:"chan_size"`
}
type Collect struct {
	LogFilePath string `ini:"logfile_path"`
}
type EtcdConfig struct {
	Address    string `ini:"address"`
	CollectKey string `ini:"collect_key"`
}

func main() {

	var ConfigObj = new(Config)
	err := ini.MapTo(ConfigObj, "./conf/config.ini")
	if err != nil {
		fmt.Printf("Fail to read file: %v", err)
		os.Exit(1)
	}
	fmt.Printf("%#v\n", ConfigObj)

	err = Kafka.InitKafka([]string{ConfigObj.KafkaConfig.Address}, ConfigObj.KafkaConfig.ChanSize)
	if err != nil {
		fmt.Printf("Init Kafka failed,err :%v\n", err)
		return
	}
	fmt.Println("Kafka init on")

	err = Etcd.EtcdInit([]string{ConfigObj.EtcdConfig.Address})
	if err != nil {
		fmt.Printf("Init Etcd failed,err :%v\n", err)
		return
	}

	// Etcd.Write()
	allConf, err := Etcd.GetConf(ConfigObj.EtcdConfig.CollectKey)

	go Etcd.WatchConf(ConfigObj.EtcdConfig.CollectKey)
	if err != nil {
		fmt.Printf("Get Etcd conf failed,err :%v\n", err)
		return
	}
	fmt.Println(allConf)

	err = Tailfile.TailInit(allConf)
	if err != nil {
		fmt.Printf("Init tail failed,err :%v\n", err)
		return
	}
	fmt.Println("tail init on")
	for {
	}
	// send log to kafka by sarama.0
}
